package connections;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import message.Message;
/**
 * this class job is to start the connection with the server 
 * @author ran
 *
 */
public class ContactToServer {
	private String host;
	private boolean connected;
	
	/**
	 * check if the client is already connected
	 * @return true if connected. false otherwise.
	 */
	public boolean isConnected() {
		return connected;
	}
	
	/**
	 * Ctor of the class
	 */
	public ContactToServer() {
		host = "localhost";
		connected=false;
	}
	/**
	 * open connection and send message to the server
	 * @param message the message to send
	 * @throws IOException
	 */
	public void sendMassege(Message message) throws IOException{
		Socket socket=null;
		try {
			// open the socket
			socket = new Socket(host,12346);
			// send the message
			SendConn sc = new SendConn(socket,message);
			Thread t = new Thread(sc);
			t.start();
		}catch (UnknownHostException e) {
			System.err.println("Dont know host: "+host);
			System.exit(-1);
		}catch (IOException e) {
			System.err.println("Cannot get IO for connection to"+host);
			System.exit(-1);
		}
		
	}
}

